#!/bin/bash

# We need to have two arrays here

# First, write the date and time to the file 'oo_stats'

echo "On `date '+%c'`" >> $HOME/printers/oo_status/oo_stats
echo "`date '+%c'`" >> $HOME/printers/oo_status/date_time

# We need to also define a variable $tally

printer_tally=0
total_tally=0

# Then, check to see if the printer status page is empty; if it is, this means that the script 'toner.sh' has not been able to retrieve the page, and the printer may
# be out of order. Just to make sure, we should check to see if the printer responds to pings, and as a final check, we need to see if the printer is available. If the 
# toner file is empty, the printer doesn't respond to pings, and the printer is available, we should mark the printer as out-of-order. Otherwise, we should just tally 
# the printer as "OK" and add it to the total of printers that are available

# Create the array for printer names

printer_names=( baskerville cleveland lib_iif lib_1st_flr lib_2nd_flr norris2 read2 \
		scilab scilib jrc_email younker2 helpdesk2 duerer gencomp2 rose ccl3_bw inslab2 )

for ((i=0; i < ${#printer_names[@]}; i++))
do

[[ -s $HOME/printers/toner_level/"${printer_names[$i]}"_level ]]

if [ `echo $?` == 1 ]
then
	ping "${printer_names[$i]}".grinnell.edu -c 4
	if [ `echo $?` == 1 ]
	then
		open_status=`cat $HOME/printers/open_statuses/"${printer_names[$i]}"`
		if [ $open_status == 1 ]
		then
			total_tally=`expr $total_tally + 1`
			printer=`echo "${printer_names[$i]}" | tr "[:lower:]" "[:upper:]"`
			echo "$printer was out-of-order" >> $HOME/printers/oo_status/oo_stats
			echo "0" >> $HOME/printers/oo_status/"${printer_names[$i]}"
		else
			echo "1" >> $HOME/printers/oo_status/"${printer_names[$i]}"
		fi
	else
		printer_tally=`expr $printer_tally + 1`
		total_tally=`expr $total_tally + 1`
		echo "1" >> $HOME/printers/oo_status/"${printer_names[$i]}"
	fi
else
	printer_tally=`expr $printer_tally + 1`
	total_tally=`expr $total_tally + 1`
	echo "1" >> $HOME/printers/oo_status/"${printer_names[$i]}"
fi
done

# As of 2008-10-13, I've been unable to use wget to automatically retrieve the printer status pages for the following printers; therefore, the only conditional I perform here # is a "ping" check and open_status check

# Create an array for these printers

other_printer_names=( lazier av1 )

for ((i=0; i < ${#other_printer_names[@]}; i++))
do

ping "${other_printer_names[$i]}".grinnell.edu -c 4
if [ `echo $?` == 1 ]
then
	open_status=`cat $HOME/printers/open_statuses/"${other_printer_names[$i]}"`
	if [ $open_status == 1 ]
	then
		total_tally=`expr $total_tally + 1`
		printer=`echo "${other_printer_names[$i]}" | tr "[:lower:]" "[:upper:]"`
		echo "$printer was out-of-order" >> $HOME/printers/oo_status/oo_stats
		echo "0" >> $HOME/printers/oo_status/"${other_printer_names[$i]}"
	else
		echo "1" >> $HOME/printers/oo_status/"${other_printer_names[$i]}"
	fi
else
	printer_tally=`expr $printer_tally + 1`
	total_tally=`expr $total_tally + 1`
	echo "1" >> $HOME/printers/oo_status/"${other_printer_names[$i]}"
fi
done

# We have to take into account the fact that Escher is a color printer... 
# all we need to do is examine whether its black ink file is empty

[[ -s $HOME/printers/toner_level/escher_black_level ]]

if [ `echo $?` == 1 ]
then
	ping escher.grinnell.edu -c 4
	if [ `echo $?` == 1 ]
	then
		open_status=`cat $HOME/printers/open_statuses/"${other_printer_names[$i]}"`
		if [ $open_status == 1 ]
		then
			total_tally=`expr $total_tally + 1`
			echo "ESCHER was out-of-order" >> $HOME/printers/oo_status/oo_stats
			echo "0" >> $HOME/printers/oo_status/escher
		else
			echo "1" >> $HOME/printers/oo_status/escher
		fi
	else
		printer_tally=`expr $printer_tally + 1`
		total_tally=`expr $total_tally + 1`
		echo "1" >> $HOME/printers/oo_status/escher
	fi
else
	printer_tally=`expr $printer_tally + 1`
	total_tally=`expr $total_tally + 1`
	echo "1" >> $HOME/printers/oo_status/escher
fi

# Finally, print the number of available printers

echo "$printer_tally out of the $total_tally public printers were available" >> $HOME/printers/oo_status/oo_stats
echo >> $HOME/printers/oo_status/oo_stats

exit 0

	
		
